<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

$api = app('Dingo\Api\Routing\Router');
$api->version('v1', [
    'namespace' => 'App\Http\Controllers\Api',
    'middleware' => 'serializer:array'
], function($api) {
    // login
    $api->post('authorizations', 'AuthorizationsController@store')->name('api.authorizations.store');
    $api->post('send_email', 'AuthorizationsController@sendEmail')->name('api.authorizations.send-email');
    // register
    $api->post('register', 'AuthorizationsController@register')->name('api.authorizations.register');

    // 需要 token 验证的接口
    $api->group(['middleware' => 'api.auth'], function($api) {

        // 谷歌验证码
        $api->post('google_2fa', 'UsersController@google_2fa')->name('api.user.google_2fa');
        $api->post('google_2fa_validate', 'UsersController@google_2fa_validate')->name('api.user.google_2fa_validate');

        // 当前登录用户信息
        $api->get('user', 'UsersController@me')->name('api.user.show');
        $api->get('user_addresses', 'UserAddressController@index')->name('user_addresses.index');
        $api->get('user_addresses/{address_id}', 'UserAddressController@show')->name('user_addresses.show');
        $api->patch('user_addresses/{address_id}', 'UserAddressController@update')->name('user_addresses.update');
        $api->delete('user_addresses/{address_id}', 'UserAddressController@destroy')->name('user_addresses.destroy');
        $api->post('user_addresses', 'UserAddressController@store')->name('user_addresses.store');
    });

    $api->group(['middleware' => 'api.auth','prefix' => 'products'], function($api) {
        $api->get('/favorites', 'ProductsController@favorites')->name('products.favorites');
        $api->post('/{product}/favorite', 'ProductsController@favor')->name('products.favor');
        $api->delete('/{product}/favorite', 'ProductsController@disfavor')->name('products.disfavor');
    });

    $api->group(['prefix' => 'products'], function($api) {
        $api->get('/', 'ProductsController@index')->name('products.index');
        $api->get('/{id}', 'ProductsController@show')->name('products.show')->where(['id' => '[0-9]+']);
    });

    // 购物车
    $api->group(['middleware' => 'api.auth','prefix' => 'cart'], function($api) {
        $api->get('/','CartController@index')->name('cart.index');
        $api->post('/', 'CartController@add')->name('cart.add');
        $api->delete('/{product_sku}', 'CartController@remove')->name('cart.remove');
    });
    // 订单
    $api->group(['middleware' => 'api.auth','prefix' => 'orders'], function($api) {
        $api->get('/', 'OrdersController@index')->name('orders.index');
        $api->get('/{id}', 'OrdersController@show')->name('orders.show');
        $api->post('/', 'OrdersController@store')->name('orders.store');
        $api->post('/{order}/received', 'OrdersController@received')->name('orders.received');  // 确认收货
        $api->post('/{order}/apply_refund', 'OrdersController@applyRefund')->name('orders.apply_refund');   // 申请退款
    });
    $api->group(['middleware' => 'api.auth','prefix' => 'coupon_codes'], function($api) {
        $api->get('/{code}', 'CouponCodesController@show')->name('coupon_codes.show');  // 优惠码是否过期
    });
});